package rsa

import (
	"crypto"
	"crypto/rand"
	"crypto/rsa"
	"crypto/sha1"
)

// RsaSha1Sign 签名数据
func RsaSha1Sign(data, key string, opts ...string) ([]byte, error) {
	hashed := sha1.Sum([]byte(data))
	priKey, err := parsePrivateKey(key)
	if err != nil {
		return nil, err
	}

	signature, err := rsa.SignPKCS1v15(rand.Reader, priKey, crypto.SHA1, hashed[:])
	if err != nil {
		return nil, err
	}
	return signature, nil
}

// RsaSha1VerySign 验证签名是否正确
func RsaSha1VerySign(data, sign string, pubKey string, opts ...string) (bool, error) {
	publicKey, err1 := parsePublicKey(pubKey)
	if err1 != nil {
		return false, err1
	}

	hashed := sha1.Sum([]byte(data))

	err := rsa.VerifyPKCS1v15(publicKey, crypto.SHA1, hashed[:], []byte(sign))
	if err != nil {
		return false, err
	}
	return true, nil
}
